home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / DirectInput / DIConfig / uiglobals.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  18.2 KB  |  772 lines

  1. //-----------------------------------------------------------------------------
  2. // File: uiglobals.cpp
  3. //
  4. // Desc: CUIGlobals is a class that packs and holds most information
  5. //       relevent to a UI session.  Many classes make reference to
  6. //       CUIGlobals all the time.
  7. //
  8. //       CPaintHelper encapsulates GDI calls, simplifying GDI operations.
  9. //
  10. // Copyright (C) 1999-2001 Microsoft Corporation. All Rights Reserved.
  11. //-----------------------------------------------------------------------------
  12.  
  13. #include "common.hpp"
  14. #define __DEFINE_ELEMENT_STRUCTURES__
  15. #include "uielements.h"
  16.  
  17.  
  18. static const GUID GUID_DIConfigAppEditLayout = 
  19. { 0xfd4ace13, 0x7044, 0x4204, { 0x8b, 0x15, 0x9, 0x52, 0x86, 0xb1, 0x2e, 0xad } };
  20.  
  21.  
  22. CUIGlobals::CUIGlobals(UIG_PARAMS_DEFINE) :
  23.  
  24.     // globals...
  25.     m_hrInit(S_OK),
  26.  
  27.     m_hrFinalResult(S_OK),
  28.  
  29.     m_hInst(NULL),
  30.     m_lpDI(NULL),
  31.  
  32.     m_dwFlags(0),
  33.     m_wszUserNames(NULL),
  34.     m_pSurface(NULL),
  35.     m_pSurface3D(NULL),
  36.     m_lpCallback(NULL),
  37.     m_pvRefData(NULL),
  38.  
  39.     m_bAllowEditLayout(FALSE),
  40.  
  41.     m_bUseColorSet(FALSE),
  42.  
  43.     // ui...
  44.     m_pElement(NULL),
  45.     m_nElements(0),
  46.     m_pFont(NULL),
  47.     m_nFonts(0),
  48.     m_pBrush(NULL),
  49.     m_nBrushes(0),
  50.     m_pPen(NULL),
  51.     m_nPens(0),
  52.     m_pColor(NULL),
  53.     m_nColors(0)
  54. {
  55.     tracescope(__ts,_T("CUIGlobals::CUIGlobals()\n"));
  56.  
  57.     m_hrInit = Init(UIG_PARAMS_DEFINE_PASS);
  58. }
  59.  
  60. void CUIGlobals::SetTableColor(UICOLOR uic, COLORREF c)
  61. {
  62.     UICOLORINFO *info = GetColorInfo(uic);
  63.     assert(info != NULL);
  64.     if (info == NULL)
  65.         return;
  66.  
  67.     info->rgb = c;
  68. }
  69.  
  70. HRESULT CUIGlobals::Init(UIG_PARAMS_DEFINE)
  71. {tracescope(__ts,_T("CUIGlobals::Init(...)\n"));
  72.  
  73.     HRESULT hr = S_OK;
  74.  
  75.     // get instance handle
  76.     m_hInst = (HINSTANCE)g_hModule;
  77.     if (m_hInst == NULL)
  78.     {
  79.         etrace(_T("hInst NULL\n"));
  80.         return E_FAIL;
  81.     }
  82.  
  83.     // create direct input
  84.     DWORD dwVer = DIRECTINPUT_VERSION;
  85.     hr = DirectInput8Create(m_hInst, dwVer, IID_IDirectInput8W, (LPVOID *)&m_lpDI, NULL);
  86.     if (FAILED(hr) || m_lpDI == NULL)
  87.     {
  88.         m_lpDI = NULL;
  89.         etrace2(_T("Could not create DirectInput ver 0x%08x\n  -> DirectInputCreateEx() returned 0x%08x\n"), dwVer, hr);
  90.         return hr;
  91.     }
  92.  
  93.     // save flags
  94.     m_dwFlags = dwFlags;
  95. #ifdef CFGUI__FORCE_NON_NULL_WSZUSERNAMES
  96.     if (wszUserNames == NULL)
  97.         wszUserNames = _T("Forced Non-NULL Username");
  98. #endif
  99.     if (wszUserNames == NULL)
  100.     {
  101.         etrace(_T("wszUserNames was passed NULL\n"));
  102.         return E_FAIL;
  103.     }
  104.  
  105.     // save user names
  106.     m_wszUserNames = DupSuperString(wszUserNames);
  107.     if (m_wszUserNames == NULL)
  108.     {
  109.         etrace(_T("Could not duplicate user names\n"));
  110.         return E_FAIL;
  111.     }
  112.  
  113.     // make sure we were passed an action format
  114.     if (lpAcFor == NULL)
  115.     {
  116.         etrace(_T("lpAcFor param NULL\n"));
  117.         return E_INVALIDARG;
  118.     }
  119.  
  120.     // copy the acfor to the master
  121.     hr = InitMasterAcForArray(lpAcFor, int(dwNumAcFor));
  122.     if (FAILED(hr))
  123.     {
  124.         etrace1(_T("InitMasterAcForArray() failed, returning 0x%08x\n"), hr);
  125.         return hr;
  126.     }
  127.  
  128.     // get surface
  129.     if (lpSurface != NULL)
  130.     {
  131.         hr = lpSurface->QueryInterface(IID_IDirect3DSurface8, (void **)&m_pSurface3D);
  132.         if (FAILED(hr) || m_pSurface3D == NULL)
  133.         {
  134.             m_pSurface3D = NULL;
  135.         }
  136.  
  137.         hr = lpSurface->QueryInterface(IID_IDirectDrawSurface, (void **)&m_pSurface);
  138.         if (FAILED(hr) || m_pSurface == NULL)
  139.         {
  140.             m_pSurface = NULL;
  141.         }
  142.  
  143.         if (m_pSurface == NULL && m_pSurface3D == NULL)
  144.             etrace(_T("lpSurface was non-NULL but could not get IDirect3DSurface8 or IID_IDirectDrawSurface from it"));
  145.     }
  146.  
  147.     // save callback and ref data
  148.     m_lpCallback = lpCallback;
  149.     m_pvRefData = pvRefData;
  150.  
  151.     // see whether or not we're allowing edit layout mode
  152.     m_bAllowEditLayout = IsEqualGUID(RefMasterAcFor(0).guidActionMap,
  153.         GUID_DIConfigAppEditLayout);
  154.  
  155.     // init a bunch of stuff necessary for painting
  156.     if (!InitColorsAndTablesAndObjects(lpDIColorSet))
  157.         return E_FAIL;
  158.  
  159.     // dump info if debug
  160. #ifdef DBG
  161.     Dump();
  162. #endif
  163.  
  164.     // return success if we got here
  165.     return S_OK;
  166. }
  167.  
  168. BOOL CUIGlobals::InitColorsAndTablesAndObjects(LPDICOLORSET lpDIColorSet)
  169. {tracescope(__ts,_T("CUIGlobals::InitColorsAndTablesAndObjects()\n"));
  170.  
  171.     // init ui tables
  172.     if (!InitTables())
  173.     {
  174.         etrace(_T("Could not initialize tables\n"));
  175.         return FALSE;
  176.     }
  177.  
  178.     // decide whether or not to use the passed colorset
  179.     if (lpDIColorSet != NULL)
  180.     {
  181.         m_ColorSet = *lpDIColorSet;
  182.  
  183.         m_bUseColorSet = !IsZeroOrInvalidColorSet(m_ColorSet);
  184.     }
  185.     else
  186.         m_bUseColorSet = FALSE;
  187.  
  188.     // use it, or use defaults
  189.     if (m_bUseColorSet)
  190.     {
  191.         // transfer colors from passed colorset
  192.         SetTableColor(UIC_TEXTFORE, D3DCOLOR2COLORREF(m_ColorSet.cTextFore));
  193.         SetTableColor(UIC_TEXTHIGHLIGHT, D3DCOLOR2COLORREF(m_ColorSet.cTextHighlight));
  194.         SetTableColor(UIC_CALLOUTLINE, D3DCOLOR2COLORREF(m_ColorSet.cCalloutLine));
  195.         SetTableColor(UIC_CALLOUTHIGHLIGHT, D3DCOLOR2COLORREF(m_ColorSet.cCalloutHighlight));
  196.         SetTableColor(UIC_BORDER, D3DCOLOR2COLORREF(m_ColorSet.cBorder));
  197.         SetTableColor(UIC_CONTROLFILL, D3DCOLOR2COLORREF(m_ColorSet.cControlFill));
  198.         SetTableColor(UIC_HIGHLIGHTFILL, D3DCOLOR2COLORREF(m_ColorSet.cHighlightFill));
  199.         SetTableColor(UIC_AREAFILL, D3DCOLOR2COLORREF(m_ColorSet.cAreaFill));
  200.     }
  201.     else
  202.     {
  203.         // use default colors
  204.         SetTableColor(UIC_TEXTFORE,                RGB(255, 255, 255));
  205.         SetTableColor(UIC_TEXTHIGHLIGHT,        RGB(  0, 255,   0));
  206.         SetTableColor(UIC_CALLOUTLINE,            RGB(255, 255, 255));
  207.         SetTableColor(UIC_CALLOUTHIGHLIGHT,        RGB(  0, 255,   0));
  208.         SetTableColor(UIC_BORDER,                RGB(255, 255,   0));
  209.         SetTableColor(UIC_CONTROLFILL,            RGB(  0, 191,   0));
  210.         SetTableColor(UIC_HIGHLIGHTFILL,        RGB(  0,   0,   0));
  211.         SetTableColor(UIC_AREAFILL,                RGB(  0,   0,   0));
  212.     }
  213.  
  214.     // create the table objects
  215.     CreateObjects();
  216.  
  217.     return TRUE;
  218. }
  219.  
  220. CUIGlobals::~CUIGlobals()
  221. {
  222.     tracescope(__ts,_T("CUIGlobals::~CUIGlobals()\n"));
  223.  
  224.     if (m_wszUserNames != NULL)
  225.         free((LPVOID)m_wszUserNames);
  226.     m_wszUserNames = NULL;
  227.  
  228.     if (m_lpDI != NULL)
  229.         m_lpDI->Release();
  230.     m_lpDI = NULL;
  231.  
  232.     if (m_pSurface != NULL)
  233.         m_pSurface->Release();
  234.     m_pSurface = NULL;
  235.  
  236.     if (m_pSurface3D != NULL)
  237.         m_pSurface3D->Release();
  238.     m_pSurface3D = NULL;
  239.  
  240.     ClearMasterAcForArray();
  241.  
  242.     ClearTables();
  243. }
  244.  
  245. void CUIGlobals::Dump()
  246. {
  247.     tracescope(ts, _T("UIGlobals...\n\n"));
  248.  
  249.     traceHEXPTR(m_hInst);
  250.     traceHEXPTR(m_lpDI);
  251.     LPTSTR str = AllocConfigureFlagStr(m_dwFlags);
  252.     trace1(_T("m_dwFlags = %s\n"), str);
  253.     free(str);
  254.     traceSUPERSTR(m_wszUserNames);
  255.     traceHEXPTR(m_pSurface);
  256.     traceHEXPTR(m_pSurface3D);
  257.     traceHEXPTR(m_lpCallback);
  258.     traceBOOL(m_bAllowEditLayout);
  259.     {
  260.         tracescope(__csts, _T("m_ColorSet...\n"));
  261.         traceHEX(m_ColorSet.cTextFore);
  262.         traceHEX(m_ColorSet.cTextHighlight);
  263.         traceHEX(m_ColorSet.cCalloutLine);
  264.         traceHEX(m_ColorSet.cCalloutHighlight);
  265.         traceHEX(m_ColorSet.cBorder);
  266.         traceHEX(m_ColorSet.cControlFill);
  267.         traceHEX(m_ColorSet.cHighlightFill);
  268.         traceHEX(m_ColorSet.cAreaFill);
  269.     }
  270.     traceBOOL(m_bUseColorSet);
  271.     trace(_T("\n"));
  272.     TraceActionFormat(_T("Master ActionFormat 0:"), RefMasterAcFor(0));
  273.     trace(_T("\n\n"));
  274. }
  275.  
  276. LPDIRECTINPUT8W CUIGlobals::GetDI()
  277. {
  278.     if (m_lpDI == NULL)
  279.         return NULL;
  280.  
  281.     m_lpDI->AddRef();
  282.     return m_lpDI;
  283. }
  284.  
  285. IDirectDrawSurface *CUIGlobals::GetSurface()
  286. {
  287.     if (m_pSurface == NULL)
  288.         return NULL;
  289.  
  290.     m_pSurface->AddRef();
  291.     return m_pSurface;
  292. }
  293.  
  294. IDirect3DSurface8 *CUIGlobals::GetSurface3D()
  295. {
  296.     if (m_pSurface3D == NULL)
  297.         return NULL;
  298.  
  299.     m_pSurface3D->AddRef();
  300.     return m_pSurface3D;
  301. }
  302.  
  303. void CUIGlobals::DeleteObjects()
  304. {
  305.     // make sure all our gdi objects are deleted
  306.     int i;
  307.     if (m_pFont != NULL)
  308.         for (i = 0; i <    m_nFonts; i++)
  309.         {
  310.             UIFONTINFO &info = m_pFont[i];
  311.             if (info.hFont != NULL)
  312.                 DeleteObject(info.hFont);
  313.             info.hFont = NULL;
  314.         }
  315.     if (m_pBrush != NULL)
  316.         for (i = 0; i <    m_nBrushes; i++)
  317.         {
  318.             UIBRUSHINFO &info = m_pBrush[i];
  319.             if (info.hBrush != NULL)
  320.                 DeleteObject(info.hBrush);
  321.             info.hBrush = NULL;
  322.             if (info.hPen != NULL)
  323.                 DeleteObject(info.hPen);
  324.             info.hPen = NULL;
  325.         }
  326.     if (m_pPen != NULL)
  327.         for (i = 0; i <    m_nPens; i++)
  328.         {
  329.             UIPENINFO &info = m_pPen[i];
  330.             if (info.hPen != NULL)
  331.                 DeleteObject(info.hPen);
  332.             info.hPen = NULL;
  333.         }
  334. }
  335.  
  336. void CUIGlobals::ClearTables()
  337. {
  338.     // make sure all our gdi objects are deleted
  339.     DeleteObjects();
  340.  
  341.     // delete the tables, null the pointers, and zero the counters
  342. #define FREETABLE(member, memnum) \
  343. { \
  344.     if (member != NULL) \
  345.         delete [] member; \
  346.     member = NULL; \
  347.     memnum = 0; \
  348. }
  349.     FREETABLE(m_pElement, m_nElements);
  350.     FREETABLE(m_pFont, m_nFonts);
  351.     FREETABLE(m_pBrush, m_nBrushes);
  352.     FREETABLE(m_pPen, m_nPens);
  353.     FREETABLE(m_pColor, m_nColors);
  354. }
  355.  
  356. BOOL CUIGlobals::InitTables()
  357. {
  358.     BOOL bSuccess = TRUE;
  359.  
  360.     // make sure the tables have been cleared
  361.     ClearTables();
  362.  
  363.     // allocate our own copies of all the tables
  364. #define ALLOCTABLE(member, memnum, type, init, num) \
  365. { \
  366.     member = new type [memnum = num]; \
  367.     if (member == NULL) \
  368.     { \
  369.         memnum = 0; \
  370.         bSuccess = FALSE; \
  371.     } \
  372.     else \
  373.         memcpy(member, init, sizeof(type) * memnum); \
  374. }
  375.     ALLOCTABLE(m_pElement, m_nElements, UIELEMENTINFO, uielement, NUMUIELEMENTS);
  376.     ALLOCTABLE(m_pFont, m_nFonts, UIFONTINFO, uifont, NUMUIFONTS);
  377.     ALLOCTABLE(m_pBrush, m_nBrushes, UIBRUSHINFO, uibrush, NUMUIBRUSHES);
  378.     ALLOCTABLE(m_pPen, m_nPens, UIPENINFO, uipen, NUMUIPENS);
  379.     ALLOCTABLE(m_pColor, m_nColors, UICOLORINFO, uicolor, NUMUICOLORS);
  380.  
  381.     return bSuccess;
  382. }
  383.  
  384. void CUIGlobals::RecreateObjects()
  385. {
  386.     DeleteObjects();
  387.     CreateObjects();
  388. }
  389.  
  390. void CUIGlobals::CreateObjects()
  391. {
  392.     // make sure all our gdi objects are created
  393.     int i;
  394.     if (m_pFont != NULL)
  395.     {
  396.         HDC hDC = GetDC(NULL);
  397.         for (i = 0; i <    m_nFonts; i++)
  398.         {
  399.             UIFONTINFO &info = m_pFont[i];
  400.             if (info.hFont == NULL)
  401.             {
  402.                 LOGFONT lf;
  403.                 lf.lfHeight = -MulDiv(info.nPointSize, GetDeviceCaps(hDC, LOGPIXELSY), 72);
  404.                 lf.lfWidth = 0;
  405.                 lf.lfEscapement = 0;
  406.                 lf.lfOrientation = 0;
  407.                 lf.lfWeight = info.bBold ? FW_BOLD : FW_NORMAL;
  408.                 lf.lfItalic = FALSE;
  409.                 lf.lfUnderline = FALSE;
  410.                 lf.lfStrikeOut = FALSE;
  411.                 lf.lfCharSet = DEFAULT_CHARSET;
  412.                 lf.lfOutPrecision = OUT_DEFAULT_PRECIS;
  413.                 lf.lfClipPrecision = CLIP_DEFAULT_PRECIS;
  414.                 lf.lfQuality = PROOF_QUALITY;
  415.                 lf.lfPitchAndFamily = DEFAULT_PITCH | FF_DONTCARE;
  416.                 _tcscpy(lf.lfFaceName, info.lfFaceName);
  417.  
  418.                 info.hFont = (HGDIOBJ)CreateFontIndirect(&lf);
  419.             }
  420.         }
  421.         ReleaseDC(NULL, hDC);
  422.     }
  423.     if (m_pBrush != NULL)
  424.         for (i = 0; i <    m_nBrushes; i++)
  425.         {
  426.             UIBRUSHINFO &info = m_pBrush[i];
  427.             if (info.hBrush == NULL)
  428.                 info.hBrush = (HGDIOBJ)CreateSolidBrush(GetColor(info.eColor));
  429.             if (info.hPen == NULL)
  430.                 info.hPen = (HGDIOBJ)CreatePen(PS_SOLID, 1, GetColor(info.eColor));
  431.         }
  432.     if (m_pPen != NULL)
  433.         for (i = 0; i <    m_nPens; i++)
  434.         {
  435.             UIPENINFO &info = m_pPen[i];
  436.             if (info.hPen == NULL)
  437.                 info.hPen = (HGDIOBJ)CreatePen(info.fnPenStyle, info.nWidth, GetColor(info.eColor));
  438.         }
  439. }
  440.  
  441.  
  442. #define IMPLGETINFO(Type, TYPE, Types, t) \
  443. UI##TYPE##INFO *CUIGlobals::Get##Type##Info(UI##TYPE t) \
  444. { \
  445.     if (m_p##Type != NULL) \
  446.         for (int i = 0; i < m_n##Types; i++) \
  447.             if (m_p##Type[i].e##Type == t) \
  448.                 return &(m_p##Type[i]); \
  449.     return NULL; \
  450. }
  451.  
  452. IMPLGETINFO(Element, ELEMENT, Elements, e)
  453. IMPLGETINFO(Font, FONT, Fonts, f)
  454. IMPLGETINFO(Brush, BRUSH, Brushes, b)
  455. IMPLGETINFO(Pen, PEN, Pens, p)
  456. IMPLGETINFO(Color, COLOR, Colors, c)
  457.  
  458. #undef IMPLGETINFO
  459.  
  460.  
  461. #define IMPLGET(T, Name, Type, TYPE, v, def, ret) \
  462. T CUIGlobals::Get##Name(UI##TYPE ui##v) \
  463. { \
  464.     UI##TYPE##INFO *v = Get##Type##Info(ui##v); \
  465.     if (!v) \
  466.         return def; \
  467.     return ret; \
  468. }
  469.  
  470. IMPLGET(HGDIOBJ, Font, Element, ELEMENT, e, NULL, GetFont(e->eFont))
  471. IMPLGET(HGDIOBJ, Font, Font, FONT, f, NULL, f->hFont)
  472. IMPLGET(HGDIOBJ, Brush, Element, ELEMENT, e, NULL, GetBrush(e->eBrush))
  473. IMPLGET(HGDIOBJ, Brush, Brush, BRUSH, b, NULL, b->hBrush)
  474. IMPLGET(HGDIOBJ, Pen, Element, ELEMENT, e, NULL, GetPen(e->ePen))
  475. IMPLGET(HGDIOBJ, Pen, Brush, BRUSH, b, NULL, b->hPen)
  476. IMPLGET(HGDIOBJ, Pen, Pen, PEN, p, NULL, p->hPen)
  477. IMPLGET(COLORREF, BrushColor, Element, ELEMENT, e, RGB(255, 127, 127), GetColor(e->eBrush))
  478. IMPLGET(COLORREF, PenColor, Element, ELEMENT, e, RGB(255, 127, 127), GetColor(e->ePen))
  479. IMPLGET(COLORREF, TextColor, Element, ELEMENT, e, RGB(255, 127, 127), GetColor(e->eText))
  480. IMPLGET(COLORREF, BkColor, Element, ELEMENT, e, RGB(255, 127, 127), GetColor(e->eBk))
  481. IMPLGET(COLORREF, Color, Brush, BRUSH, b, RGB(255, 127, 127), GetColor(b->eColor))
  482. IMPLGET(COLORREF, Color, Pen, PEN, p, RGB(255, 127, 127), GetColor(p->eColor))
  483. IMPLGET(COLORREF, Color, Color, COLOR, c, RGB(255, 127, 127), c->rgb)
  484.  
  485. #undef IMPLGET
  486.  
  487.  
  488. CPaintHelper::CPaintHelper(CUIGlobals &uig, HDC hDC) :
  489.     m_uig(uig), m_priv_hDC(hDC), m_hDC(m_priv_hDC),
  490.     m_eFont(UIF_VOID),
  491.     m_eBrush(UIB_VOID),
  492.     m_ePen(UIP_VOID),
  493.     m_eText(UIC_VOID),
  494.     m_eBk(UIC_VOID),
  495.     m_hOldFont(NULL), m_hOldBrush(NULL), m_hOldPen(NULL),
  496.     m_bOldFont(FALSE), m_bOldBrush(FALSE), m_bOldPen(FALSE)
  497. {
  498.     if (m_hDC != NULL)
  499.     {
  500.         m_oldtextcolor = GetTextColor(m_hDC);
  501.         m_oldbkcolor = GetBkColor(m_hDC);
  502.         m_oldbkmode = GetBkMode(m_hDC);
  503.     }
  504. }
  505.  
  506. CPaintHelper::~CPaintHelper()
  507. {
  508.     if (m_hDC != NULL)
  509.     {
  510.         if (m_bOldFont)
  511.             SelectObject(m_hDC, m_hOldFont);
  512.         if (m_bOldBrush)
  513.             SelectObject(m_hDC, m_hOldBrush);
  514.         if (m_bOldPen)
  515.             SelectObject(m_hDC, m_hOldPen);
  516.  
  517.         SetTextColor(m_hDC, m_oldtextcolor);
  518.         SetBkColor(m_hDC, m_oldbkcolor);
  519.         SetBkMode(m_hDC, m_oldbkmode);
  520.     }
  521. }
  522.  
  523. void CPaintHelper::SetElement(UIELEMENT eElement)
  524. {
  525.     UIELEMENTINFO *info = m_uig.GetElementInfo(eElement);
  526.     if (!info)
  527.         return;
  528.  
  529.     if (info->eFont != UIF_LAST)
  530.         SetFont(info->eFont);
  531.     if (info->eBrush != UIB_LAST)
  532.         SetBrush(info->eBrush);
  533.     if (info->ePen != UIP_LAST)
  534.         SetPen(info->ePen);
  535.     SetText(info->eText, info->eBk);
  536. }
  537.  
  538. void CPaintHelper::SetFont(UIFONT eFont)
  539. {
  540.     if (m_eFont == eFont || eFont == UIF_LAST)
  541.         return;
  542.  
  543.     HGDIOBJ hObj = m_uig.GetFont(eFont);
  544.     if (hObj == NULL)
  545.         return;
  546.  
  547.     if (m_hDC != NULL)
  548.     {
  549.         HGDIOBJ hOld = NULL;
  550.         hOld = SelectObject(m_hDC, hObj);
  551.         if (!m_bOldFont)
  552.             m_hOldFont = hOld;
  553.         m_bOldFont = TRUE;
  554.     }
  555.  
  556.     m_eFont = eFont;
  557. }
  558.  
  559. void CPaintHelper::SetBrush(UIBRUSH eBrush)
  560. {
  561.     if (m_eBrush == eBrush || eBrush == UIB_LAST)
  562.         return;
  563.  
  564.     HGDIOBJ hObj = eBrush == UIB_NULL ?
  565.         GetStockObject(NULL_BRUSH) :
  566.         m_uig.GetBrush(eBrush);
  567.     if (hObj == NULL)
  568.         return;
  569.  
  570.     if (m_hDC != NULL)
  571.     {
  572.         HGDIOBJ hOld = NULL;
  573.         hOld = SelectObject(m_hDC, hObj);
  574.         if (!m_bOldBrush)
  575.             m_hOldBrush = hOld;
  576.         m_bOldBrush = TRUE;
  577.     }
  578.  
  579.     m_eBrush = eBrush;
  580. }
  581.  
  582. void CPaintHelper::SetPen(UIPEN ePen)
  583. {
  584.     if (m_ePen == ePen || ePen == UIP_LAST)
  585.         return;
  586.  
  587.     HGDIOBJ hObj = ePen == UIB_NULL ?
  588.         GetStockObject(NULL_PEN) :
  589.         m_uig.GetPen(ePen);
  590.     if (hObj == NULL)
  591.         return;
  592.  
  593.     if (m_hDC != NULL)
  594.     {
  595.         HGDIOBJ hOld = NULL;
  596.         hOld = SelectObject(m_hDC, hObj);
  597.         if (!m_bOldPen)
  598.             m_hOldPen = hOld;
  599.         m_bOldPen = TRUE;
  600.     }
  601.  
  602.     m_ePen = ePen;
  603. }
  604.  
  605. void CPaintHelper::SetText(UICOLOR eText, UICOLOR eBk)
  606. {
  607.     if (m_eText != eText && eText != UIC_LAST)
  608.     {
  609.         if (m_hDC != NULL)
  610.             SetTextColor(m_hDC, m_uig.GetColor(eText));
  611.         m_eText = eText;
  612.     }
  613.     if (m_eBk != eBk && eBk != UIC_LAST)
  614.     {
  615.         if (m_hDC != NULL)
  616.         {
  617.             if (eBk == UIC_NULL)
  618.                 SetBkMode(m_hDC, TRANSPARENT);
  619.             else 
  620.             {
  621.                 SetBkColor(m_hDC, m_uig.GetColor(eBk));
  622.                 SetBkMode(m_hDC, OPAQUE);
  623.             }
  624.         }
  625.         m_eBk = eBk;
  626.     }
  627. }
  628.  
  629. BOOL CPaintHelper::LineTo(int x, int y)
  630. {
  631.     if (m_hDC == NULL)
  632.         return FALSE;
  633.  
  634.     return ::LineTo(m_hDC, x, y);
  635. }
  636.  
  637. BOOL CPaintHelper::MoveTo(int x, int y, SPOINT *last)
  638. {
  639.     if (m_hDC == NULL)
  640.         return FALSE;
  641.  
  642.     POINT p;
  643.     BOOL bRet = MoveToEx(m_hDC, x, y, &p);
  644.     if (last)
  645.         *last = p;
  646.     return bRet;
  647. }
  648.  
  649. BOOL CPaintHelper::Rectangle(SRECT r, UIRECTTYPE eType)
  650. {
  651.     // fail on no dc
  652.     if (m_hDC == NULL)
  653.         return FALSE;
  654.  
  655.     // see if we lack a pen or brush (might add more checks later)
  656.     BOOL bNoPen = m_ePen == UIP_NULL;
  657.     BOOL bNoBrush = m_eBrush == UIB_NULL;
  658.  
  659.     // fail if trying to do an outline without a pen
  660.     if (eType == UIR_OUTLINE && bNoPen)
  661.         return FALSE;
  662.  
  663.     // fail if trying to do a solid without a brush
  664.     if (eType == UIR_SOLID && bNoBrush)
  665.         return FALSE;
  666.  
  667.     // save old objects if we change anything...
  668.     HGDIOBJ hOldBrush = NULL, hOldPen = NULL;
  669.  
  670.     // select a null brush if we're doing an outline and we're not already null brushed
  671.     if (eType == UIR_OUTLINE && m_eBrush != UIB_NULL)
  672.         hOldBrush = SelectObject(m_hDC, GetStockObject(NULL_BRUSH));
  673.  
  674.     // select a pen the same color as the current brush if doing solid
  675.     if (eType == UIR_SOLID || m_ePen == UIP_NULL)
  676.     {
  677.         HGDIOBJ hPen = m_uig.GetPen(m_eBrush);
  678.         if (hPen == NULL)
  679.             return FALSE;
  680.         hOldPen = SelectObject(m_hDC, hPen);
  681.     }
  682.  
  683.     // draw the rect
  684.     BOOL bRet = ::Rectangle(m_hDC, r.left, r.top, r.right, r.bottom);
  685.  
  686.     // restore whatever changed
  687.     if (eType == UIR_OUTLINE && m_eBrush != UIB_NULL)
  688.         SelectObject(m_hDC, hOldBrush);
  689.     if (eType == UIR_SOLID || m_ePen == UIP_NULL)
  690.         SelectObject(m_hDC, hOldPen);
  691.  
  692.     return bRet;
  693. }
  694.  
  695. const DIACTIONFORMATW &CUIGlobals::RefMasterAcFor(int i)
  696. {
  697.     assert(IsValidMasterAcForIndex(i));
  698.     return m_MasterAcForArray[i];
  699. }
  700.  
  701. BOOL CUIGlobals::IsValidMasterAcForIndex(int i)
  702. {
  703.     if (i < 0 || i >= m_MasterAcForArray.GetSize())
  704.         return FALSE;
  705.  
  706.     return TRUE;
  707. }
  708.  
  709. HRESULT CUIGlobals::InitMasterAcForArray(const DIACTIONFORMATW *af, int n)
  710. {
  711.     if (n < 1)
  712.         return E_FAIL;
  713.  
  714.     ClearMasterAcForArray();
  715.  
  716.     m_MasterAcForArray.SetSize(n);
  717.  
  718.     for (int i = 0; i < n; i++)
  719.     {
  720.         HRESULT hr = CopyActionFormat(m_MasterAcForArray[i], af[i]);
  721.         if (FAILED(hr))
  722.         {
  723.             m_MasterAcForArray.SetSize(i);
  724.             ClearMasterAcForArray();
  725.  
  726.             return hr;
  727.         }
  728.     }
  729.  
  730.     return S_OK;
  731. }
  732.  
  733. void CUIGlobals::ClearMasterAcForArray()
  734. {
  735.     int s = m_MasterAcForArray.GetSize();
  736.  
  737.     for (int i = 0; i < s; i++)
  738.         CleanupActionFormatCopy(m_MasterAcForArray[i]);
  739.  
  740.     m_MasterAcForArray.RemoveAll();
  741.     assert(m_MasterAcForArray.GetSize() == 0);
  742. }
  743.  
  744. LPCWSTR CUIGlobals::GetUserName(int i)
  745. {
  746.     return GetSubString(m_wszUserNames, i);
  747. }
  748.  
  749. int CUIGlobals::GetNumUserNames()
  750. {
  751.     return CountSubStrings(m_wszUserNames);
  752. }
  753.  
  754. void CUIGlobals::SetFinalResult(HRESULT hr)
  755. {
  756.     m_hrFinalResult = hr;
  757. }
  758.  
  759. HRESULT CUIGlobals::GetFinalResult()
  760. {
  761.     return m_hrFinalResult;
  762. }
  763.  
  764. int CUIGlobals::GetUserNameIndex(LPCWSTR wsz)
  765. {
  766.     for (int i = 0; i < GetNumUserNames(); i++)
  767.         if (_wcsicmp(wsz, GetUserName(i)) == 0)
  768.             return i;
  769.  
  770.     return -1;
  771. }
  772.